<?php

namespace App\Http\Controllers\Guard\Flight;

use App\Http\Controllers\Common\GuardController;
use App\Http\Requests\AddFlightRequest;
use App\Models\Flight\FlightInfo;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class FlightInfoController extends GuardController{

    private $model = FlightInfo::class;

    public function edit(Request $request){
        $ids = $request->input('ids',[]);
        $edit_fly_time = $request->input('edit_fly_time');
        FlightInfo::query()->whereIn('id',$ids)->update([
            'start_fly_time'=>$edit_fly_time[0],
            'end_fly_time'=>$edit_fly_time[1],
        ]);
        return responseSuccess();
    }

    public function lst(Request $request) {

        if($request->has('able')){
            $date = $request->input('date');
            $records = (new $this->model())->where('date','=',$date)->get();
            foreach ($records as $record) {
                $record->label = $record->flight_no;
                $record->value = $record->id;
            }
            return responseSuccess($records->toArray());
        }

        $currentUser = $this->getCurrentGuardUser($request);
        $ids = $currentUser->getBelowUser();
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',20);

        $conditions = $request->input();

        $records = (new $this->model())->getLst($conditions)->whereIn('writer_id',$ids)
            ->with('getWriter:id,name')->paginate($pageSize,$this->getCol,'page',$page);

        foreach ($records as $record) {
            $record->date = Str::replace('-', '/', $record->date);
        }

        $currentAuth = $currentUser->role_ids??'';
        $currentAuth = empty($currentAuth)?[]:explode(',', $currentAuth);
        return responseSuccess($records->items(),$records->total(),100000,compact('currentAuth'));

    }

    public function add(AddFlightRequest $request) {
        $is_double = $request->input('is_double');
        $flight_dates = $request->input('flight_dates');
        $flight_nos = $request->input('flight_nos');
        $info = $request->input();
        $guardUser = $this->getCurrentGuardUser($request);
        $time = date('Y-m-d H:i:s');
        try {
            foreach ($flight_dates as $date) {

                if(FlightInfo::query()->where(['date'=>$date,'flight_no'=>$flight_nos[0]])->exists()){
                    return responseFail("$date 下 $flight_nos[0] 已存在!");
                }
                if(FlightInfo::query()->where(['date'=>$date,'flight_no'=>$flight_nos[1]])->exists()){
                    return responseFail("$date 下 $flight_nos[1] 已存在!");
                }
                if($is_double){
                    $data = [
                        [
                            'date'=>$date,
                            'flight_no'=>$flight_nos[0],
                            'start_fly_time'=>$info['fly_time1'][0],
                            'end_fly_time'=>$info['fly_time1'][1],
                            'start_airport'=>$info['start_airport1'],
                            'end_airport'=>$info['end_airport1'],
                            'couple_flight_no'=>$flight_nos[1],
                            'writer_id'=> $guardUser->id,
                            'created_at'=>$time,
                            'updated_at'=>$time
                        ],
                        [
                            'date'=>$date,
                            'flight_no'=>$flight_nos[1],
                            'start_fly_time'=>$info['fly_time2'][0],
                            'end_fly_time'=>$info['fly_time2'][1],
                            'start_airport'=>$info['start_airport2'],
                            'end_airport'=>$info['end_airport2'],
                            'couple_flight_no'=>$flight_nos[0],
                            'writer_id'=> $guardUser->id,
                            'created_at'=>$time,
                            'updated_at'=>$time
                        ],
                    ];
                }else{
                    $data = [
                        [
                            'date'=>$date,
                            'flight_no'=>$flight_nos[0],
                            'start_fly_time'=>$info['fly_time1'][0],
                            'end_fly_time'=>$info['fly_time1'][1],
                            'start_airport'=>$info['start_airport1'],
                            'end_airport'=>$info['end_airport1'],
                            'writer_id'=> $guardUser->id,
                            'created_at'=>$time,
                            'updated_at'=>$time
                        ]
                    ];
                }
                FlightInfo::query()->insert($data);
            }
            return responseSuccess();
        }catch (\Exception $e){
            $error = $e->getFile().':'.$e->getLine().':'.$e->getMessage();
            return responseFail($error);
        }
    }

    public function detail(Request $request){
        $id = $request->input('id',0);
        $records = $this->getDetail($this->model,[$id]);
        return responseSuccess($records->toArray(),1);
    }

    public function change(Request $request) {

        $ids = $request->input('ids',[]);
        $status = $request->input('status');
        return $this->changeColByIds($this->model, [$ids],compact('status'));
    }

    public function del(Request $request) {
        $ids = $request->input('ids',[]);
        if(!is_array($ids))$ids = [$ids];
        return $this->delRecords($this->model, $ids);
    }
}
